home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15524 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.0 KB

  1. Path: news.ahc.ameritech.com!datalytics!usenet
  2. From: Rob Stewart <stew@datalytics.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: (no subject)
  5. Date: Fri, 05 Apr 1996 18:44:07 -0500
  6. Organization: Datalytics, Inc
  7. Message-ID: <3165B047.12E6@datalytics.com>
  8. References: <4juvuk$4vn@newserv.ksu.ksu.edu>
  9. NNTP-Posting-Host: 204.62.224.71
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. Iyer wrote:
  16. > I have a problem in returning a two dimensional array.I have declared it
  17. > as a two dimensional pointer in my Matrix class.This is a private
  18. > variable.To access this i have two functions .One is
  19. > void SetMatrixElements(double **mat)
  20. > double **GetMatElements();
  21. > When i receive this GetMatElements(),i am declaring a double **temp,
  22. > then I am receiving in this temp pointer.But i  am not allocating any
  23. > memory for this temp pointer.When i delete the the Matrix object, it's
  24. > mat elements get deleted,but to which this temp is pointing.But i cannot
  25. > delete the temp before i quit the function,this will delete the mat
  26. > elements.So, what shall i do.
  27. > class Matrix
  28. > {
  29. >   private:
  30. > int nc;int nr; double **mat;
  31. > public:
  32. > double **GetMatElemnts();
  33. > void SetMatElemnts(double **m);
  34. > }
  35. > Matrix *A;
  36. > void Function(){
  37. > double **temp;
  38. > temp = A->GetMatElements();
  39. > }
  40.  
  41. Returning a non-const pointer to a private dm is poor practice.  
  42. That aside, the problem is that GetMatElements is returning a 
  43. pointer to the same heap allocation as Matrix::mat points to.  
  44. If you choose to retain the current design, you will need 
  45. GetMatElements to allocate and populate a second array as a copy 
  46. of mat.  It can then return the pointer to this copy.  As 
  47. parallel behavior, SetMatElements must first "delete [] mat" 
  48. before assigning m to mat.
  49.  
  50. This implementation causes an ownership problem.  If you call 
  51. GetMatElements to get a pointer to an array, modify one or more 
  52. elements, then call SetMatElements with that pointer, who owns 
  53. the memory?  The caller has a pointer from GetMatElements which 
  54. is known to be a copy of Matrix's mat data member.  Therefore, 
  55. the caller should "delete [] temp."  However, the caller used 
  56. that pointer (temp) in a call to SetMatElements.  SetMatElements 
  57. then deleted its previous matrix and set mat to point to the 
  58. matrix given to it.  Therefore, SetMatElements took ownership of 
  59. the memory.
  60.  
  61. The only safe implementation for this is to use a vector class 
  62. to manage the data.  vector is part of STL, so it's reasonable 
  63. to expect that you don't have it available.  However, you can 
  64. write your own one dimensional array class overloading operator 
  65. [] to access the elements.  The result would be the following 
  66. declaration:
  67.  
  68.     vector<vector<double>> matrix;
  69.  
  70. To access a specific element, you simply do this:
  71.  
  72.     matrix[n][m]
  73.  
  74. Now, you can read and write individual elements with clear 
  75. syntax just as you would write them on paper.
  76.  
  77. -- 
  78. Robert Stewart        | My opinions are usually my own.
  79. Datalytics, Inc.    | stew@datalytics.com
  80.